# Maintainer: David Runge <dvzrv@archlinux.org>

_name=wally
pkgbase=zsa-wally
pkgname=(zsa-wally zsa-udev)
pkgver=2.1.3
pkgrel=4
pkgdesc="Flash your ZSA Keyboard the EZ way"
arch=(loong64 x86_64)
url="https://github.com/zsa/wally"
license=(MIT)
makedepends=(glib2 go gtk3 libusb npm ripgrep wails webkit2gtk)
source=($pkgname-$pkgver.tar.gz::https://github.com/zsa/$_name/archive/refs/tags/$pkgver-linux.tar.gz)
sha512sums=('f8afbef55e108986aeb5b97e924c1b98492b4ab3bca08fe56b32a21c047a085de663b0ac86724c2104ab63bb19bfdc023f4cff29a0a5284515f0d77ec9354201')
b2sums=('6ee3e77c900c8feadb540dffd45c5de9005af2793f14edceb5d8328b7472b0fa40433b72bedaed5d060a59c9c5e2f10bd0393de32f585dd480e3cee92d14cdac')

prepare() {
  cd $_name-$pkgver-linux
  # update the go.mod file to match the system wails version
  go mod edit -require github.com/wailsapp/wails@"$(wails version | rg '(v)[\d.]+' | awk '{print $(NF)}')"

  cd frontend
  npm update
  npm audit fix || printf "Unfixable problems detected\n"
}

build() {
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw"

  cd $_name-$pkgver-linux
  wails build -verbose
}

package_zsa-wally() {
  depends+=(glibc libgdk-3.so libglib-2.0.so libgobject-2.0.so libgtk-3.so
  libjavascriptcoregtk-4.0.so libusb-1.0.so libwebkit2gtk-4.0.so zsa-udev)

  cd $_name-$pkgver-linux
  install -vDm 755 build/$_name -t "$pkgdir/usr/bin/"
  install -vDm 644 dist/linux64/$_name.desktop -t "$pkgdir/usr/share/applications/"
  install -vDm 644 appicon.png "$pkgdir/usr/share/pixmaps/$_name.png"
  install -vDm 644 license.md "$pkgdir/usr/share/licenses/$pkgname/license.md"
}

package_zsa-udev() {
  pkgdesc="Udev rules for ZSA Keyboards"

  cd $_name-$pkgver-linux
  install -vDm 644 dist/linux64/*.rules -t "$pkgdir/usr/lib/udev/rules.d/"
  install -vDm 644 license.md "$pkgdir/usr/share/licenses/$pkgname/license.md"
}
